package io.chenglicun.modules.clc.controller.dataset;

import com.alibaba.excel.EasyExcel;
import io.chenglicun.common.annotation.LogOperation;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.utils.ExcelUtils;
import io.chenglicun.common.utils.Result;
import io.chenglicun.common.validator.AssertUtils;
import io.chenglicun.common.validator.ValidatorUtils;
import io.chenglicun.common.validator.group.AddGroup;
import io.chenglicun.common.validator.group.DefaultGroup;
import io.chenglicun.common.validator.group.UpdateGroup;
import io.chenglicun.modules.clc.dto.dataset.ClcDataColumnDTO;
import io.chenglicun.modules.clc.excel.dataset.ClcDataColumnExcel;
import io.chenglicun.modules.clc.excel.dataset.ClcDataColumnImportExcel;
import io.chenglicun.modules.clc.service.dataset.ClcDataColumnService;
import io.chenglicun.modules.clc.service.dataset.ClcDataSetColumnService;
import io.chenglicun.modules.sys.excel.ExcelDataOwnerInfoListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 数据字段
 *
 * @author HFHQ 1986866856@qq.com
 * @since 1.0 2024-02-26
 */
@RestController
@RequestMapping("clc/clcdatacolumn")
@Api(tags = "数据字段")
public class ClcDataColumnController {
    @Autowired
    private ClcDataColumnService clcDataColumnService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "keyStr", value = "关键字筛选", paramType = "query", dataType = "String")
    })
    public Result<PageData<ClcDataColumnDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<ClcDataColumnDTO> page = clcDataColumnService.page(params);

        return new Result<PageData<ClcDataColumnDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<ClcDataColumnDTO> get(@PathVariable("id") Long id) {
        ClcDataColumnDTO data = clcDataColumnService.get(id);

        return new Result<ClcDataColumnDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody ClcDataColumnDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        clcDataColumnService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody ClcDataColumnDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        clcDataColumnService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        clcDataColumnService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<ClcDataColumnDTO> list = clcDataColumnService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "数据字段", list, ClcDataColumnExcel.class);
    }

    @GetMapping("listall")
    @ApiOperation("所有列表")
    @LogOperation("所有列表")
    public Result<List<ClcDataColumnDTO>> listall() {
        List<ClcDataColumnDTO> list = clcDataColumnService.list(null);
        return new Result<List<ClcDataColumnDTO>>().ok(list);
    }

    @Autowired
    private ClcDataSetColumnService clcDataSetColumnService;

    @GetMapping("listbyset")
    @ApiOperation("所有列表")
    @LogOperation("所有列表")
    public Result<List<ClcDataColumnDTO>> listbyset(@ApiIgnore @RequestParam Map<String, Object> params) {
        List<ClcDataColumnDTO> list = clcDataColumnService.listbyset(params);
        return new Result<List<ClcDataColumnDTO>>().ok(list);
    }

    @PostMapping("import")
    @ApiOperation("导入")
    @LogOperation("导入")
    public Result<String> groupStatisticsImport(@RequestParam("file") MultipartFile file) throws Exception {
        ExcelDataOwnerInfoListener<ClcDataColumnImportExcel> excelDataNewListener = new ExcelDataOwnerInfoListener<>();
        //解析并保存到数据库
        EasyExcel.read(file.getInputStream(), ClcDataColumnImportExcel.class, excelDataNewListener).sheet().doRead();
        List<ClcDataColumnImportExcel> list = excelDataNewListener.getDatas();
        clcDataColumnService.importExcel(list);
        return new Result<String>().ok("导入完成");
    }

}
